package team.first.dataimport;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import org.neo4j.driver.Session;
import team.first.util.DriverUtil;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 然然然儿呐_
 * @Project_name: project
 * @Package_name: team.first
 * @Date: 2021/12/3 11:16
 * @Description:
 */
public class MovieGenreImport {
    public static void movieGenreImport() {
        Session session = DriverUtil.getNewSession();
        try {
            List<Entity> movieGenreList = Db.use().query("select * from movie_genre");

            String addMovieGenre = "MATCH (m:Movie{id:$movieId})" +
                    "MATCH (g:Genre{id:$genreId})" +
                    "CREATE (m) - [:HAS_GENRE{id:$id}] -> (g)";
            movieGenreList.forEach(movieGenre -> {
                Map<String, Object> params = new HashMap<>();
                params.put("id", movieGenre.getInt("id"));
                params.put("movieId", movieGenre.getInt("movie_id"));
                params.put("genreId", movieGenre.getInt("genre_id"));
                session.run(addMovieGenre, params);
            });
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DriverUtil.close(session);
        }
    }
}
